<?php

namespace App\Admin\Grids;

use App\Admin\Actions\DeleteAction;
use App\Admin\Actions\HospitalAppointVaccineDeleteAction;
use App\Admin\Repositories\AppointViewRule;
use App\Models\Hospital;
use App\Models\HospitalAppointType;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Form;
use App\Admin\Field\IntervalWeekNum;
use App\Admin\Forms\HospitalAppointVaccineForm;
use App\Models\HospitalAppointInterval;
use App\Models\HospitalAppointIntervalNum;
use App\Hospital\Repositories\HospitalAppointVaccine;
use App\Models\Vaccine;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\GridAction;
use Dcat\Admin\Grid\LazyRenderable;
use Dcat\Admin\Grid\RowAction;
use Illuminate\Support\Facades\DB;

class AppointViewRuleGrid extends LazyRenderable
{

    public function addScript()
    {
        Admin::script(<<<JS

        JS
        );
    }

    public function grid(): Grid
    {
        $this->addScript();
        $data = $this->payload;
        return Grid::make(new AppointViewRule(), function (Grid $grid) use ($data) {

            $grid->disableFilter();
            $grid->disableFilterButton();

            $grid->model()->where('rule_id', $data['rule_id']);


            $grid->column('type')->using(\App\Models\AppointViewRule::$typeText);
            $grid->column('value','数值')->display(function () {

                switch ($this->type) {
                    case 1:
                        return HospitalAppointType::query()->where('id', $this->value)->value('name');
                    case 2:
                        return Hospital::query()->where('id', $this->value)->value('name');
                    case 3:
                        return $this->value . "天";
                    case 4:
                        return $this->value . "天";
                    case 5:
                        return date('Y年m月d日', $this->value);
                    case 6:
                        return date('Y年m月d日', $this->value);
                    case 7:
                        return Vaccine::query()->where('id', $this->value)->value('name');
                }

            });

            // 也可以通过这种方式添加
            $grid->actions(function ($actions) {

                $actions->append(new DeleteAction('appoint-view-rule'));
                //$actions->append(new VaccineIntervalAction());

            });
            $grid->showCreateButton();
            $grid->enableDialogCreate();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableFilterButton();

            $grid->setResource('appoint-view-rule');
            $grid->disableFilter();

            $grid->setActionClass(Grid\Displayers\Actions::class);
            $grid->showQuickEditButton();
            $grid->disableViewButton();
            $grid->disableRowSelector();
            //$grid->setDialogFormDimensions('50%', '50%');

            $grid->model()->setConstraints([
                'rule_id' => $data['rule_id'],
                'weight' => $data['weight'],
            ]);


        });
    }

}
